package interfaces.menu.joinGame.gamesTable;

import fileHandling.language.Lang;
import fileHandling.language.interfaces.JoinGameText;
import interfaces.GUISource;
import interfaces.menu.joinGame.JoinGameContent;
import interfaces.menu.joinGame.gamesTable.GamesTableButton.OrderMode;
import interfaces.tables.Table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.fenggui.IWidget;
import org.fenggui.StandardWidget;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.util.Color;

public class GamesTable extends Table<GamesTableEntry> {
	
	public enum Column { ServerName, CurrentMap, GameMode, Players, Ping }
	
	public static final Color GAMES_TABLE_BG_COLOR = new Color(230, 230, 230);
	
	private static final Lang[] COLUMNS = new Lang[] { 
					JoinGameText.Server_Name, JoinGameText.Current_Map, 
					JoinGameText.Game_Mode, JoinGameText.Players, JoinGameText.Ping };
	private static final float[] WIDTHS = new float[] { 0.3f, 0.2f, 0.2f, 0.2f, 0.1f };
	
	private List<GamesTableButton> headButtons;
	private List<GamesTableWidget> tableWidgets;
	private HashMap<GamesTableWidget, GamesTableEntry> widgetEntryMap;
	private GamesTableButton currentSortedButton;
	private JoinGameContent window;
	
	public GamesTable(int width, int height, JoinGameContent window) {
		super(width, height, 0, 0, COLUMNS, WIDTHS);
		this.window = window;
		
		tableWidgets = new ArrayList<GamesTableWidget>();
		widgetEntryMap = new HashMap<GamesTableWidget, GamesTableEntry>();
		
		layout();
	}
	
	@Override
	protected void initHeader() {
		headButtons = new ArrayList<GamesTableButton>();
		super.initHeader();
	}
	
	@Override
	public void mouseClicked(IWidget widget) {
		super.mouseClicked(widget);
		GamesTableEntry entry = (GamesTableEntry)widget;
		window.setSelectedGame(entry.getGamesTableWidget());
	}
	
	@Override
	public void mouseDoubleClicked(IWidget widget) {
		super.mouseDoubleClicked(widget);
		window.joinGame();
	}
	
	public void order(Column column, boolean ascending) {
		Collections.sort(tableWidgets, new GamesTableComparator(column, ascending));
		clearTableEntries();
		for(GamesTableWidget w : tableWidgets) {
			addTableEntry(widgetEntryMap.get(w));
		}
		
		for(GamesTableButton button : headButtons) {
			if(button != currentSortedButton && button.getColumn() != column) 
				button.changeOrderMode(OrderMode.NotOrdered);
		}
		layout();
	}
	
	@Override
	protected StandardWidget getNextHeadWidget(Lang lang) {
		Column column = null;
		
		switch((JoinGameText)lang) {
			case Server_Name : column = Column.ServerName; break;
			case Current_Map : column = Column.CurrentMap; break;
			case Players : column = Column.Players; break;
			case Game_Mode : column = Column.GameMode; break;
			case Ping : column = Column.Ping; break;
		}
		
		assert(column != null);
		
		GamesTableButton button = new GamesTableButton(lang, this, column, getHeaderFont());
		headButtons.add(button);
		return button;
	}
	
	public void addGame(GamesTableWidget gameWidget) {
		tableWidgets.add(gameWidget);
		GamesTableEntry entry = new GamesTableEntry(entryWidth, labelHeight, 0, this, gameWidget);
		widgetEntryMap.put(gameWidget, entry);
		addTableEntry(entry);
	}
	
	public GamesTableWidget getSelectedGameWidget() {
		if(selectedEntry == null) return null;
		return selectedEntry.getGamesTableWidget();
	}
	
	@Override
	protected int getBorder() { return 0; }
	
	@Override
	protected int getHeaderHeight() { return getHeaderFont().getHeight() / 3 * 4; }
	
	@Override
	protected Color getHeaderTextColor() { return Color.BLACK; }
	
	@Override
	protected Color getHeaderBGColor() { return Color.LIGHT_GRAY; }
	
	@Override
	protected Color getTableBGColor() { return GAMES_TABLE_BG_COLOR; }
	
	@Override
	public ImageFont getHeaderFont() { return GUISource.middleFont; }
	
	@Override
	public boolean allowSelection() { return true; }
	
	public void clear() {
		clearTableEntries();
		tableWidgets.clear();
		widgetEntryMap.clear();
	}
}